﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Net;
using System.Data;
using System.IO;
using System.Xml;

namespace VoIPWeb.Uc
{
    public partial class RestrictUc : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            m_lbl_none.Text = "";
            if (Session["Login"] == null)
            {
                Response.Redirect("~/Login.aspx", true);
            }
            var v_str_user = Session["User"];
            var v_str_customer = Session["Customer"];
            //var v_str_cpasswd = Session["Cpassword"];
            var v_str_cpasswd = "samsung5568";
            var v_str_api = string.Format("https://www.voipinfocenter.com/api/request.ashx?command=CallOverview&username={0}&password={1}&customer={2}*{0}", v_str_user, v_str_cpasswd, v_str_customer);

            HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create(v_str_api);
            HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();
            XmlDataDocument dataXML = new XmlDataDocument();
            dataXML.Load(HttpWResp.GetResponseStream());
            XmlNodeList v_xml_node_list = dataXML.SelectNodes("//Call");

            //XmlNode v_xml_node = v_xml_node_list[0];
            // không có cuộc gọi nào phát sinh
            if (v_xml_node_list.Count == 0)
            {
                m_lbl_none.Text = "There is no call...";
                m_lbl_none.Visible = true;
            }
            else
            {
                var webClient = new WebClient();
                var wResponse = webClient.DownloadString(v_str_api);
                this.m_xml_datasoure_last_ten_calls.Data = wResponse;

                m_grv_recentCall.DataSourceID = m_xml_datasoure_last_ten_calls.ID;
                m_grv_recentCall.DataBind();
                m_lbl_none.Visible = false;
            }
            //m_dt_data_table = get_today_call_info();
            //m_lbl_none.Visible = true;
           // m_lbl_none.Text = m_dt_data_table.Rows[0]["Calls"].ToString();
        }

        #region Members
        public string m_str_email, m_str_account;
        public decimal m_dc_balance;
        DataTable m_dt_data_table = new DataTable();
        #endregion
        #region Private Methods

        private string get_url(string ip_str_user, string ip_str_customer)
        {
            string v_str_password = Session["Cpassword"].ToString();
            return string.Format("https://www.voipinfocenter.com/api/request.ashx?command=GetUserInfo&username={0}&password={1}&customer={2}*{0}", ip_str_user, v_str_password, ip_str_customer);
        }

        private DataTable get_today_call_info()
        {
            DataTable v_dt_today_call = new DataTable();
            XmlDocument v_xml_doc_today = new XmlDocument();
            HttpWebRequest v_httpwebreq = (HttpWebRequest)WebRequest.Create(get_url(Session["User"].ToString(), Session["Customer"].ToString()));
            HttpWebResponse v_httpwebrespon = (HttpWebResponse)v_httpwebreq.GetResponse();
            v_xml_doc_today.Load(v_httpwebrespon.GetResponseStream());
            string v_str_xpath = string.Format("//Call");
            // Lấy tất cả các node
            XmlNodeList v_xml_nodelist = v_xml_doc_today.SelectNodes(v_str_xpath);
            foreach (XmlNode v_xml_node in v_xml_nodelist)
            {
                v_xml_node.SelectSingleNode(".[@]");
            }

            // Lấy ra giá trị và so sánh
            v_dt_today_call.Columns.Add("Calls", typeof(int));
            DataRow v_dr = v_dt_today_call.NewRow();
            v_dr["Calls"] = v_xml_nodelist.Count;
            v_dt_today_call.Rows.Add(v_dr);
            return v_dt_today_call;
        }
        #endregion
    }
}